package fragmentson;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.wq.cycling.DemoApplication;
import com.wq.cycling.R;

import activity.SonActivity;
import bean.MessageForSimple;
import fragmentson.backhandle.BackHandledFragment;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import service.ApiUtil;

/**
 * 信息编辑
 * author：wangqi on 2017/4/26 17:48
 * email：773630555@qq.com
 */
public class EditInfo extends BackHandledFragment {

	private View rootView;
	private EditText et_nice;
	private String nice;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		rootView = getLayoutInflater(savedInstanceState).inflate(R.layout.fragment_editinfo, null);
		nice = getArguments().getString("nice");
		initView();
		return rootView;
	}

	private void initView() {
		// TODO Auto-generated method stub
		((TextView) rootView.findViewById(R.id.tv_title)).setText(R.string.userinfo_nice);
		rootView.findViewById(R.id.tv_next).setOnClickListener(listener);
		et_nice = ((EditText) rootView.findViewById(R.id.et_nice));
		et_nice.setText(nice);
	}

	public static EditInfo newInstance(String nice) {
		EditInfo fragment = new EditInfo();
		Bundle bundle = new Bundle();
		bundle.putString("nice",nice);
		fragment.setArguments(bundle);
		return fragment;
	}

	private View.OnClickListener listener = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			switch (v.getId()){
				case R.id.tv_next:
					ApiUtil.getApiService().save(DemoApplication.getToken(),et_nice.getText().toString()).enqueue(new Callback<MessageForSimple>() {
						@Override
						public void onResponse(Call<MessageForSimple> call, Response<MessageForSimple> response) {
							try {
								MessageForSimple entity = response.body();
								if(entity.getCode().equals("10008")){
									InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
									imm.hideSoftInputFromWindow(et_nice.getWindowToken(), 0);
									((SonActivity) getActivity()).backTask();
								}else{
									Toast.makeText(getActivity(),entity.getMsg(),Toast.LENGTH_LONG).show();
								}
							} catch (Exception e) {
								e.printStackTrace();
							}
						}
						@Override
						public void onFailure(Call<MessageForSimple> call, Throwable t) {
							Toast.makeText(getActivity(),getResources().getString(R.string.error),Toast.LENGTH_LONG).show();
						}
					});
					break;
				default:
					break;
			}
		}
	};

	@Override
	public boolean onBackPressed() {
		return false;
	}
	
}
